<template>
  <div>
    <el-row class="magTop" type="flex" justify="space-between">
      <el-button type="primary" @click="addPages">添加页面</el-button>
      <el-row type="flex">
        <el-input v-model="title_contains" size="medium " placeholder="请输入关键字" />
        <el-button @click="shai">筛选</el-button>
      </el-row>
    </el-row>
    <!-- <el-row> -->
    <el-table
      :data="tableData"
      border
      style="width: 100%"
    >
      <el-table-column
        prop="title"
        label="标题"
        width="333"
      />
      <el-table-column
        prop="content"
        label="内容"
        width="333"
      />
      <el-table-column
        label="操作"
      >
        <template v-slot="{row}">
          <el-button size="small" type="primary" @click="editFn(row.id)">编辑</el-button>
          <el-button size="small" type="danger" @click="delFn(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      layout="prev, pager, next"
      :total="total"
      :page-size="limit"
      :current-page="page"
      @current-change="currentChange"
    />
    <!-- </el-row> -->
    <el-dialog :title="title" :visible="showDialog" @close="closeFn">
      <el-form ref="form" :model="formData" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="标题" prop="title">
          <el-input v-model="formData.title" />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quill-editor v-model="formData.content" @blur="validate" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="confirmFn">立即创建</el-button>
          <el-button @click="closeFn">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
// import { VueEditor } from 'vue2-editor'
import { addPages, getPagesId, editPages } from '@/api/pages'

import { delPages, getPages, getTotal } from '@/api/pages.js'
export default {
  data() {
    return {
      title: '',
      formData: {
        title: '',
        content: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' },
          { min: 1, max: 250, message: '长度在 1 到 250 个字符', trigger: 'blur' }
        ]

      },
      showDialog: false,
      tableData: [],
      total: 0,
      limit: 2,
      start: 0,
      page: 1,
      title_contains: ''
      // current: 0
    }
  },
  async created() {
    this.getPages()
    this.getTotal()
    // console.log(68, this.total)
  },
  methods: {
    // 获取总条数
    async getTotal() {
      const res = await getTotal()
      this.total = res.data
    },
    // 数据回显
    async getPagesDetail(id) {
      const res = await getPagesId(id)
      this.formData = res.data
      // console.log(this.formData)
    },
    async confirmFn() {
      await this.$refs.form.validate()
      if (this.formData.id) {
        await editPages(this.formData)
      } else {
        await addPages(this.formData)
        // this.$refs.form.resetFields(this.validate())
        this.getTotal()
      }
      this.getPages()
      this.closeFn()
    },
    // 关闭弹窗
    closeFn() {
      this.showDialog = false
      this.formData = {
        title: '',
        content: ''
      }
      this.$refs.form.resetFields()
    },
    validate() {
      // 手动校验内容
      this.$refs.form.validateField('blur')
    },
    async shai() {
      // console.log(this.title_contains)
      const res = await getTotal({
        title_contains: this.title_contains
      })
      this.total = res.data
      // console.log(this.total, 99)
      this.getPages()
      // this.getPages(this.tableData = res)
      // this.getPages()
    },
    delFn(id) {
      this.$confirm('确定删除此页面吗?', '删除', {
        type: 'warning'
      }).then(async() => {
        // console.log('确定删除')
        await delPages(id)
        if (this.tableData.length === 1 && this.page !== 1) {
          this.start = (this.page - 2) * this.limit
          this.page--
        }
        this.getTotal()
        // console.log(this.start, 100)
        // 更新列表
        this.getPages()
      }).catch(() => {
        console.log('取消删除')
      })
    },
    editFn(id) {
      this.showDialog = true
      this.title = '编辑权限'
      // // 调用获取权限详情函数
      this.getPagesDetail(id)
    },
    async getPages() {
      const res = await getPages(
        {
          _limit: this.limit,
          _start: this.start,
          title_contains: this.title_contains
        }
      )
      this.tableData = res.data
      // console.log(this.start, 99)
    },
    addPages() {
      this.showDialog = true
      this.title = '新增页面'
    },
    currentChange(curPage) {
      // console.log(curPage, 11)
      this.page = curPage
      this.start = (this.page - 1) * this.limit
      // console.log(107, this.start)
      this.getPages()
    }
  }
}
</script>

<style lang="scss" scoped>
.el-input--medium {
  width: 400px;
}
.magTop{
 margin: 10px 15px 15px 15px;
}
.el-button {
    height: 36px;
}
.el-pagination{
  padding: 2px 480px;
}
.el-table--border{
  margin: 1px 15px;
}
</style>
